package cn.jy.machine.service.impl;

import cn.jy.common.index.MapChart;
import cn.jy.common.machine.domain.ExpressBoxes;
import cn.jy.common.machine.domain.ExpressMachineInfo;
import cn.jy.common.machine.domain.ExpressVendingMachines;
import cn.jy.common.machine.domain.dto.ExpressVendingMachinesDto;
import cn.jy.common.machine.domain.vo.ExpressVendingMachinesVo;
import cn.jy.machine.common.SnowFlake;
import cn.jy.machine.mapper.ExpressBoxesMapper;
import cn.jy.machine.mapper.ExpressMachineInfoMapper;
import cn.jy.machine.mapper.ExpressVendingMachinesMapper;
import cn.jy.machine.service.ExpressVendingMachinesService;
import cn.jy.machine.util.BoxCalculator;
import com.ruoyi.common.core.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ExpressVendingMachinesServiceImpl implements ExpressVendingMachinesService {
    @Autowired
    private ExpressVendingMachinesMapper expressVendingMachinesMapper;
    @Autowired
    private ExpressMachineInfoMapper expressMachineInfoMapper;
    @Autowired
    private ExpressBoxesMapper expressBoxesMapper;

    @Override
    public List<ExpressVendingMachinesDto> list(ExpressVendingMachinesVo vo) {
        return expressVendingMachinesMapper.list(vo);
    }

    @Override
    public Integer count(ExpressVendingMachinesVo vo) {
        return expressVendingMachinesMapper.count(vo);
    }

    @Override
    public void updateStatus(ExpressVendingMachines machines) {
        ExpressVendingMachines findById=expressVendingMachinesMapper.findById(machines.getId());
        /*判断柜机状态 如果是未开启状态 则修改状态   添加开启时间*/
        if(findById.getMachineStatus()==4){
            findById.setOpenDate(DateUtils.getNowDate());
            findById.setMachineStatus(0);
        }else{
            findById.setMachineStatus(machines.getMachineStatus());
        }
        expressVendingMachinesMapper.updateMachines(findById);
    }

    @Override
    public void addMachine(ExpressVendingMachinesDto dto) {
        /*生成sn*/
        long sn = SnowFlake.nextId();
        dto.setSn(String.valueOf(sn));
        /*添加柜机*/
        expressVendingMachinesMapper.insert(dto);
        /*根据sn查找机柜*/
        ExpressVendingMachines machines= expressVendingMachinesMapper.selectMachineBySn(dto.getSn());
        /*创建一个机柜信息对象*/
        ExpressMachineInfo info = new ExpressMachineInfo();
        /*赋值 属性*/
        info.setMachineId(machines.getId());
        if (dto.getPrimaryDepth()!=null){
            info.setPrimaryDepth(dto.getPrimaryDepth());
        }
        if (dto.getPrimaryHeight()!=null){
            info.setPrimaryHeight(dto.getPrimaryHeight());
        }
        if (dto.getPrimaryWidth()!=null){
            info.setPrimaryWidth(dto.getPrimaryWidth());
        }
        if (dto.getSecondaryDepth()!=null){
            info.setSecondaryDepth(dto.getSecondaryDepth());
        }
        if (dto.getSecondaryHeight()!=null){
            info.setSecondaryHeight(dto.getSecondaryHeight());
        }
        if (dto.getSecondaryWidth()!=null){
            info.setSecondaryWidth(dto.getSecondaryWidth());
        }
        /*添加柜机信息*/
        expressMachineInfoMapper.insert(info);

        Integer primary_width = (dto.getPrimaryWidth()==null ? 58 : dto.getPrimaryWidth());
        Integer primary_depth = (dto.getPrimaryDepth()==null ? 55 : dto.getPrimaryDepth());
        Integer primary_height = (dto.getPrimaryHeight()==null ? 190 : dto.getPrimaryHeight());
        Integer secondary_width = (dto.getSecondaryWidth()==null ? 136 : dto.getSecondaryWidth());
        Integer secondary_depth = (dto.getSecondaryDepth()==null ? 55 : dto.getSecondaryDepth());
        Integer secondary_height = (dto.getSecondaryHeight()==null ? 190 : dto.getSecondaryHeight());
        Integer totalWidth =primary_width+secondary_width;
        Integer totalHeight =primary_height+secondary_height;
        Integer totalDepth =primary_depth+secondary_depth;
        /*批量添加箱格*/
        List<ExpressBoxes> boxes=addBoxes(machines.getId(),totalWidth,totalHeight,totalDepth);
        expressBoxesMapper.insertBatch(boxes);
    }

    @Override
    public void save(List<ExpressVendingMachines> list) {
        for (ExpressVendingMachines machine : list) {
            /*生成sn*/
            long sn = SnowFlake.nextId();
            /*赋值属性*/
            machine.setSn(String.valueOf(sn));
            /*添加机柜*/
            expressVendingMachinesMapper.save(machine);
            /*根据sn查找机柜*/
            ExpressVendingMachines machines= expressVendingMachinesMapper.selectMachineBySn(machine.getSn());
            ExpressMachineInfo info = new ExpressMachineInfo();
            /*赋值属性*/
            info.setMachineId(machines.getId());
            /*添加柜机信息*/
            expressMachineInfoMapper.insert(info);
            /*批量添加箱格*/
            List<ExpressBoxes> boxes=addBoxes(machines.getId(),194,380,110);
            expressBoxesMapper.insertBatch(boxes);
        }
    }

    @Override
    public ExpressVendingMachinesDto findMachineById(Integer id) {
        return expressVendingMachinesMapper.findAllById(id);
    }

    @Override
    public List<MapChart> getMapChart() {
        return expressVendingMachinesMapper.getMapChart();
    }

    /*
    * 计算生成箱格规格信息
    * */
    private static List<ExpressBoxes> addBoxes(Integer machineId, Integer totalWidth, Integer totalHeight, Integer totalDepth) {

        List<ExpressBoxes> expressBoxes = new ArrayList<>();
        int boxNumber = 1; // 箱格编号起始值

        // 假设箱格类型分布：6个大箱，7个中箱，7个小箱
        int[] boxTypes = {2, 1, 0}; // 对应大箱、中箱、小箱的类型值

        for (int type : boxTypes) {
            int count = (type == 2) ? 6 : 7; // 大箱6个，中箱和小箱各7个
            for (int i = 0; i < count; i++) {
                if (expressBoxes.size() >= 20) break; // 如果已经达到20个箱格，退出循环
                ExpressBoxes box = new ExpressBoxes();
                box.setMachineId(machineId); // 柜机ID
                box.setBoxNumber(boxNumber++);
                box.setIsType(type);
                box.setStorageType(type == 2 ? 1 : 0); // 大箱的storageType为1，中箱和小箱的storageType为0
                int temperature = -18;
                BigDecimal bigDecimal = new BigDecimal(temperature);
                box.setTemperature(type == 2 ? bigDecimal : null); // 大箱的temperature为-18，中箱和小箱的temperature为null
                expressBoxes.add(box);
            }
        }

        // 如果箱格数量不足20个，随机添加直到满足条件
        while (expressBoxes.size() < 20) {
            ExpressBoxes box = new ExpressBoxes();
            box.setMachineId(machineId);
            box.setBoxNumber(boxNumber++);
            int randomType = (int) (Math.random() * boxTypes.length); // 随机选择箱格类型
            box.setIsType(boxTypes[randomType]);
            box.setStorageType(boxTypes[randomType] == 2 ? 1 : 0); // 设置storageType
            int temperature = -18;
            BigDecimal bigDecimal = new BigDecimal(temperature);
            box.setTemperature(boxTypes[randomType] == 2 ? bigDecimal : null); // 设置temperature
            expressBoxes.add(box);
        }

        // 使用 BoxCalculator 计算箱格尺寸和位置信息
        BoxCalculator.calculateBoxDimensionsAndPosition(expressBoxes, totalWidth, totalHeight, totalDepth);
        return expressBoxes;
    }
}
